package com.da.javatest.niuke.jianzhi;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author chenlida
 * @date 2020/10/22 16:52
 * @description
 */
public class C34Solution {
    public int FirstNotRepeatingChar(String str) {
        Map<Character, Integer> map = new LinkedHashMap<>(64);
        char[] chars = str.toCharArray();
        for (char c : chars) {
            map.merge(c, 1, Integer::sum);
        }
        for (Character c : map.keySet()) {
            if (map.get(c) == 1) {
                for (int i = 0; i < chars.length; i++) {
                    if (chars[i] == c) {
                        return i;
                    }
                }
            }
        }
        return -1;
    }

    public int FirstNotRepeatingChar2(String str) {
        if (str == null || str.length() == 0) {
            return -1;
        }
        if (str.length() == 1) {
            return 1;
        }
        char[] charArr = str.toCharArray();
        int[] count = new int[128];

        for (char c : charArr) {
            count[c]++;
        }
        for (int i = 0; i < count.length; i++) {
            if (count[charArr[i]] == 1) {
                return i;
            }
        }
        return -1;
    }
}
